package cn.nawang.ebeim.client.version;

import org.junit.Assert;

import java.util.Arrays;

/**
 * Given an array and a value, remove all instances of that:
 * value in place and return the new length.
 * The order of elements can be changed.
 * It doesn't matter what you leave beyond the new length.
 * Created by GanJc on 2016/2/24.
 */
public class RemoveElement {

    public static void main(String[] args) {
        int a[] = {1, 2, 2, 3, 2, 4};
        Assert.assertEquals(3,removeElement(a, 2));
    }

    public static int removeElement(int[] a, int elem) {
        int j = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] == elem) {
                continue;
            }
            a[j] = a[i];
            j++;
        }
        System.out.println(Arrays.toString(a));
        return j;
    }
}
